﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Folder.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Folder class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    
    /// <summary>
    /// Defines the HFolder class.
    /// </summary>
    public class Folder
    {
        #region Fields
        /// <summary>
        /// The identity of the database table's row where this folder object is saved.
        /// </summary>
        private int folderID;

        /// <summary>
        /// Stores the folder's virtual path.
        /// </summary>
        private string folderpath;
        #endregion

        #region Constructors
        /// <summary>
        /// Initializes a new instance of the Folder class.
        /// </summary>
        public Folder()
        {
        }

        /// <summary>
        /// Initializes a new instance of the Folder class.
        /// </summary>
        /// <param name="folderId">The identity of the database table's row referencing this folder.</param>
        public Folder(int folderId)
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFolderByID", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FolderID", folderId);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            this.folderID = folderId;
                            this.folderpath = rdr["FolderPath"].ToString();
                        }
                        else
                        {
                            throw new DataException("Folder does not exist.");
                        }
                    }
                }
            }
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the folder's database row identity.
        /// </summary>
        public int FolderId
        {
            get
            {
                return this.folderID;
            }
        }

        /// <summary>
        /// Gets or sets the folder's virtual path.
        /// </summary>
        public string FolderPath
        {
            get
            {
                return this.folderpath;
            }

            set
            {
                this.folderpath = value;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Finds a folder based on its path, if it exists.
        /// </summary>
        /// <param name="path">The virtual path to search for.</param>
        /// <returns>If the folder is found, a folder is populated and returned. Returns null otherwise.</returns>
        public static Folder GetFolderByPath(string path)
        {
            Folder h = null;
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetFolderIDFromFolderPath", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FolderPath", path);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            rdr.Read();
                            h = new Folder(Convert.ToInt32(rdr["FolderID"], CultureInfo.InvariantCulture));
                        }
                    }
                }
            }

            return h;
        }
        
        /// <summary>
        /// Inserts the HFolder object into the database.
        /// </summary>
        public void SaveAs()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertFolder", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FolderPath", this.folderpath);
                    this.folderID = Convert.ToInt32(cmd.ExecuteScalar(), CultureInfo.InvariantCulture);
                }
            }
        }

        /// <summary>
        /// Updates the HFolder's database fields.
        /// </summary>
        public void Save()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.UpdateFolder", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FolderID", this.folderID);
                    cmd.Parameters.AddWithValue("@FolderPath", this.folderpath);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        /// <summary>
        /// Deletes the HFolder's database entry.
        /// </summary>
        public void Delete()
        {
            using (SqlConnection conn = new SqlConnection(Environmental.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteFolder", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@FolderPath", this.folderpath);
                    cmd.ExecuteNonQuery();
                }
            }
        }
        #endregion
    }
}